package pers.dawnyang.modular.system.controller;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import pers.dawnyang.common.annotation.OperLog;
import pers.dawnyang.common.domain.Result;
import pers.dawnyang.common.domain.entity.SysDictType;
import pers.dawnyang.common.enums.OperType;
import pers.dawnyang.common.util.ResultUtil;
import pers.dawnyang.modular.system.domain.request.dictType.DictTypeReqC;
import pers.dawnyang.modular.system.domain.request.dictType.DictTypeReqR;
import pers.dawnyang.modular.system.domain.request.dictType.DictTypeReqU;
import pers.dawnyang.modular.system.service.DictTypeService;

/**
 * 字典管理
 * 
 * @author dawn yang
 * 
 */
@RequestMapping("/sys/dictType")
@Controller
@Validated
public class DictTypeController {

  @Autowired
  private DictTypeService dictTypeService;

  @RequiresPermissions("sys:dict:index")
  @GetMapping("/index")
  public String index() {
    return "/sys/dictType/index";
  }

  @RequiresPermissions("sys:dict:add")
  @GetMapping("/addView")
  public String addView() {
    return "/sys/dictType/addView";
  }

  @RequiresPermissions("sys:dict:edit")
  @GetMapping("/editView")
  public String editView(@RequestParam("id") Long id, ModelMap model) {
    SysDictType sysDictType = dictTypeService.getById(id);
    model.put("model", JSON.toJSONString(sysDictType));
    return "/sys/dictType/editView";
  }

  @RequiresPermissions("sys:dict:list")
  @PostMapping("/getPage")
  @ResponseBody
  public Result<Page<SysDictType>> getPage(Page<SysDictType> page, DictTypeReqR req) {
    LambdaQueryWrapper<SysDictType> lqw = new LambdaQueryWrapper<>();
    lqw.like((StringUtils.isNotBlank(req.getTypeName())), SysDictType::getTypeName,
        req.getTypeName());
    lqw.like((StringUtils.isNotBlank(req.getTypeCode())), SysDictType::getTypeCode,
        req.getTypeCode());
    lqw.eq((null != req.getStatus()), SysDictType::getStatus, req.getStatus());
    lqw.orderByAsc(SysDictType::getSort);
    Page<SysDictType> pageRes = dictTypeService.page(page, lqw);
    return ResultUtil.success(pageRes);
  }

  @OperLog(operModul = "字典管理", operType = OperType.UPDATE)
  @RequiresPermissions("sys:dict:edit")
  @PostMapping("/edit")
  @ResponseBody
  public Result<String> edit(@Validated DictTypeReqU req) {
    SysDictType sysDictType = new SysDictType();
    BeanUtils.copyProperties(req, sysDictType);
    dictTypeService.updateById(sysDictType);
    // DictUtils.clearByTypeCode(req.getTypeCode());
    dictTypeService.initDictCache();
    return ResultUtil.success();
  }

  @OperLog(operModul = "字典管理", operType = OperType.INSERT)
  @RequiresPermissions("sys:dict:add")
  @PostMapping("/add")
  @ResponseBody
  public Result<String> add(@Validated DictTypeReqC req) {
    SysDictType sysDictType = new SysDictType();
    BeanUtils.copyProperties(req, sysDictType);
    dictTypeService.save(sysDictType);
    dictTypeService.initDictCache();
    return ResultUtil.success();
  }

  @OperLog(operModul = "字典管理", operType = OperType.DELETE)
  @RequiresPermissions("sys:dict:del")
  @PostMapping("/del")
  @ResponseBody
  public Result<String> del(@RequestParam("typeCode") String typeCode) {
    LambdaQueryWrapper<SysDictType> lqw = new LambdaQueryWrapper<>();
    lqw.eq(SysDictType::getTypeCode, typeCode);
    SysDictType sysDictType = new SysDictType();
    sysDictType.setStatus(0);
    dictTypeService.update(sysDictType, lqw);
    // DictUtils.clearByTypeCode(typeCode);
    dictTypeService.initDictCache();
    return ResultUtil.success();
  }

  @OperLog(operModul = "字典管理", operType = OperType.OTHER)
  @RequiresPermissions("sys:dict:index")
  @PostMapping("/initDictCache")
  @ResponseBody
  public Result<String> initDictCache() {
    dictTypeService.initDictCache();
    return ResultUtil.success();
  }

}
